/**
 * Copyright phonemap.com
 *
 * Licensed under the phonemap.com License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.phonemap.com/licenses/LICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

/**
 * Required
 */
//@import script/new/phonmap.global.1.0.js
//@import PhonemapLogin
//@import PhonemapConstant


var actionPage;

/**
 * Constructor.
 * @param page {String}
 */
function PhonemapLocation(page) {
	 // allow instantiation without 'new'
    if (!(this instanceof PhonemapLocation)) return new PhonemapLocation(page);
    actionPage = page;
    
    /**
     * Do geolocation.
     */
    PhonemapLocation.prototype.doGeolocation = function() {
    	if (navigator.geolocation) {
    		//FIXME
    		alert('geolocation');
    		navigator.geolocation
    				.getCurrentPosition(positionSuccess, positionError);
    	} else {
    		positionError(-1);
    	}
    };
    
    /**
     * Position Success.
     * @param position
     */
    function positionSuccess(position) {
    	// Centre the map on the new location
    	var coords = position.coords || position.coordinate || position;
    	var curPos = new google.maps.LatLng(coords.latitude, coords.longitude);
    	//FIXME
    	alert(curPos);
    	if (actionPage == PhonemapConstant.LOGIN_PAGE) {
    		PhonemapLogin.insertFacebookUser(curPos);
    	} else if (actionPage == PhonemapConstant.FOLLOW_PAGE) {
    		PhonemapLogin.updatePosition(curPos, PhonemapConstant.FOLLOW_PAGE);
    	} else if (actionPage == PhonemapConstant.SEARCH_PAGE) {
    		PhonemapLogin.updatePosition(curPos, PhonemapConstant.SEARCH_PAGE);
    	} else if (actionPage == PhonemapConstant.LOOK_AROUND_PAGE) {
    		PhonemapLogin.updatePosition(curPos, PhonemapConstant.LOOK_AROUND_PAGE);
    	}
    }
    
}

/**
 * Position error.
 * @param err
 */
function positionError(err) {
	//FIXME
	alert('geolocation error:' + err.code);
	switch (err.code) {
		case err.UNKNOWN_ERROR:
			msg = "Unable to find your location";
			break;
		case err.PERMISSION_DENINED:
			msg = "Permission denied in finding your location";
			break;
		case err.POSITION_UNAVAILABLE:
			msg = "Your location is currently unknown";
			break;
		case err.BREAK:
			msg = "Attempt to find location took too long";
			break;
		default:
			msg = "You need to enable location on browser to continue...";
	}
	document.getElementById('status').innerHTML = msg;
}

/**
 * Contains.
 * @param array
 * @param item
 * @returns {Boolean}
 */
function contains(array, item) {
	for (var i = 0, I = array.length; i < I; ++i) {
		if (array[i] == item)
			return true;
	}
	return false;
}







